package com.sznaner.tool.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.MediaMetadataRetriever;
import android.os.Handler;
import android.widget.ImageView;

public class ImageUtil {

    public static Handler handler = new Handler();

    public static Bitmap getImageBitmap(String path){
        if(path == null) return null;
        BitmapFactory.Options options = new BitmapFactory.Options();
        Bitmap bm = BitmapFactory.decodeFile(path, options);
        return bm;
    }
    public static Drawable getImageDrawable(String filePath) {
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inPreferredConfig = Bitmap.Config.RGB_565;
        opt.inPurgeable = true;
        opt.inInputShareable = true;
        Bitmap bitmap = BitmapFactory.decodeFile(filePath,opt);
        Drawable drawable = new BitmapDrawable(NRTool.context().getResources(), bitmap);
        return drawable;
    }
    public  static int  getImageResourceForMipmap(String imageName){
        int resId = NRTool.context().getResources().getIdentifier(imageName, "mipmap", NRTool.context().getPackageName());
        //如果没有在"mipmap"下找到imageName,将会返回0
        return resId;
    }
    public  static int  getImageResourceForDrawable(String imageName){
        int resId = NRTool.context().getResources().getIdentifier(imageName, "drawable", NRTool.context().getPackageName());
        //如果没有在"Drawable"下找到imageName,将会返回0
        return resId;
    }



    //从歌曲本地路径,获取并加载封面
    public static void loadMusicCover(ImageView imageView,String musicPath,int defImage) {

        if(musicPath == null){
            imageView.setImageResource(defImage);
            return;
        }
        MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
        mediaMetadataRetriever.setDataSource(musicPath);
        byte[] picture = mediaMetadataRetriever.getEmbeddedPicture();
        if(picture != null){
            Bitmap bitmap = BitmapFactory.decodeByteArray(picture, 0, picture.length);
            if(bitmap !=null){
                imageView.setImageBitmap(bitmap);
                return;
            }
        }
        //没获取到,给默认
        imageView.setImageResource(defImage);
    }

    //根据歌曲路径,加载歌曲模糊封面
    public static void loadMusicCoverBlur( ImageView imageView,String musicPath,int defImage) {
        int scaleRatio = 6;//10是缩放的倍数（越大模糊效果越高）
        if(musicPath == null){//没有歌曲路径,取默认图片
            Bitmap scaledBitmap = BitmapFactory.decodeResource(NRTool.context().getResources(), defImage);
            Bitmap blurBitmap = FastBlurUtil.toBlur(scaledBitmap, scaleRatio);
            imageView.setImageBitmap(blurBitmap);
            return;
        }
        //获取歌曲模糊封面
        MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
        mediaMetadataRetriever.setDataSource(musicPath);
        byte[] picture = mediaMetadataRetriever.getEmbeddedPicture();
        if(picture != null) {
            Bitmap bitmap = BitmapFactory.decodeByteArray(picture, 0, picture.length);
            if (bitmap != null) {
                Bitmap blurBitmap = FastBlurUtil.toBlur(bitmap, scaleRatio);
                imageView.setImageBitmap(blurBitmap);
                return;
            }
        }

        //没有获取到,给默认模糊封面
        Bitmap scaledBitmap = BitmapFactory.decodeResource(NRTool.context().getResources(), defImage);
        Bitmap blurBitmap = FastBlurUtil.toBlur(scaledBitmap, scaleRatio);
        imageView.setImageBitmap(blurBitmap);
    }


    //根据图片路径加载图片
    public static void loadImage(ImageView imageView,String path){
        BitmapFactory.Options options = new BitmapFactory.Options();
        Bitmap bm = BitmapFactory.decodeFile(path, options);
        imageView.setImageBitmap(bm);
    }

    //根据图片路径,加载模糊效果图片
    public static void loadImageBlur( ImageView imageView, String path,int defImage){
        int scaleRatio = 6;//10是缩放的倍数（越大模糊效果越高）

        BitmapFactory.Options options = new BitmapFactory.Options();
        Bitmap bitmap = BitmapFactory.decodeFile(path, options);
        if(bitmap !=null) {
            Bitmap blurBitmap = FastBlurUtil.toBlur(bitmap, scaleRatio);
            if(blurBitmap != null){
                imageView.setImageBitmap(blurBitmap);
                return;
            }
        }
        //没获取到,就给默认模糊封面defImage
        Bitmap scaledBitmap = BitmapFactory.decodeResource(NRTool.context().getResources(), defImage);
        Bitmap blurBitmap = FastBlurUtil.toBlur(scaledBitmap, scaleRatio);
        imageView.setImageBitmap(blurBitmap);
    }

    //加载网络图片-模糊效果
    public static void loadImageBlurWithHttpUrl(final ImageView imageView, final String url) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                final Bitmap bitmap = FastBlurUtil.GetUrlBitmap(url, 8);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        imageView.setImageBitmap(bitmap);
                    }});
            }
        }).start();
    }
    public static Drawable zoomDrawable(Drawable drawable, int w, int h) {
        int width = drawable.getIntrinsicWidth();
        int height = drawable.getIntrinsicHeight();
        Bitmap oldbmp = drawableToBitmap(drawable);
        Matrix matrix = new Matrix();
        float scaleWidth = ((float) w / width);
        float scaleHeight = ((float) h / height);
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height,
                matrix, true);
        return new BitmapDrawable(null, newbmp);
    }
    public static Bitmap drawableToBitmap(Drawable drawable) {
        int width = drawable.getIntrinsicWidth();
        int height = drawable.getIntrinsicHeight();
        Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                : Bitmap.Config.RGB_565;
        Bitmap bitmap = Bitmap.createBitmap(width, height, config);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, width, height);
        drawable.draw(canvas);
        return bitmap;
    }

}
